<?php

namespace app\admin\controller;

use app\admin\common\controller\Base;
use app\common\model\Apply as Applymodel;
use Sms\SmsSingleSender;
use think\facade\Request;
use think\facade\Session;

class Index extends Base {
	public function index() {
		$this->redirect('index/login');
	}

	public function login() {
		$this->assign('title', '管理员登录');
		return $this->view->fetch('login');
	}

	public function checklogin() {
		$data = Request::param();
		// halt($data);

		if ($data['username'] == 'admin' && $data['password'] == '123456') {
			Session::set('username', 'admin');
			// $this->success('登录成功','index/index');
			$this->redirect('index/apply');
		};
		$this->error('登录失败');
	}

	public function logout() {
		Session::clear();
		$this->redirect('index/login');
	}

	public function apply() {
		$this->islogin();
		$this->assign('empty', '暂无数据');
		$this->assign('title', '申请列表');
		$applylst = Applymodel::paginate(10);
		// halt($applylst);
		$this->assign('applylst', $applylst);
		return $this->view->fetch('apply');
	}

	public function applycheck() {
		$applyid = Request::param('id');
		$res = Applymodel::get($applyid);
		$this->assign('title', '申请信息审核');
		$this->assign('res', $res);
		return $this->view->fetch('applycheck');
	}

	public function passsms($phoneNumber) {
		$appid = 1400097064;
		$appkey = "e2e90aa5a942aba0a94eb952a3eb75b7";
		// $phoneNumber = "15038168635";
		$templId = 133195;
		$sender = new SmsSingleSender($appid, $appkey);
		$result = $sender->send(0, "86", $phoneNumber, "恭喜您已经通过郑大一附院进修报名网上审核，请您携带现场确认材料，于6月21日（全天）来郑州大学第一附属医院河医院区3号楼21楼教育处完成现场确认。");
		// $rsp = json_decode($result);
		// halt($result);
	}
	public function nopasssms($phoneNumber, $msg) {
		$appid = 1400097064;
		$appkey = "e2e90aa5a942aba0a94eb952a3eb75b7";
		// $phoneNumber = "15038168635";
		$templId = 133197;
		$params = $msg;
		$sender = new SmsSingleSender($appid, $appkey);
		$result = $sender->sendWithParam("86", $phoneNumber, $templId, $params, "");
		// $rsp = json_decode($result);
		// halt($result);
	}

	public function savecheck() {

		$data = Request::param();
		$result = Applymodel::update($data);

		if ($result) {
			// $this->singlesms();
			$id = Request::param('id');
			$status = implode(Applymodel::where('id', $id)->column('status'));
			$phoneNumber = implode(Applymodel::where('id', $id)->column('telphone'));
			$msg = Applymodel::where('id', $id)->column('shenhemsg');
			// halt($msg);
			if ($status == '2') {
				$this->passsms($phoneNumber);
			}
			if ($status == '1') {
				$this->nopasssms($phoneNumber, $msg);
			}
			$this->success('审核成功', 'admin/index/apply');
		} else {
			$this->error('审核失败请重试');
		}
	}

	public function detaile() {
		$id = Request::param('id');
		$res = Applymodel::get($id);
		$this->assign('title', '申请详情');
		$this->assign('res', $res);
		return $this->view->fetch('detaile');
	}

	public function export() {
		require_once '../extend/excel/PHPExcel.php';
		$obj = new \PHPExcel();
		$applylst = model("common/Apply")->where('status', 2)->select()->toArray();
		// halt($applylst);
		$fileName = time();
		$title = array('ID', '姓名', '性别', '身份证号', '手机号', '职称', '进修时长', '进修科室', '工作单位', '工作单位联系人', '单位负责人电话', '执业医师证号', '执业范围', '特殊情况说明', '申请时间', '状态', '审核时间', '审核意见');

		$activeSheet = $obj->getActiveSheet(0);
		//横向单元格标识
		$cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');

		$_row = 1; //设置纵向单元格标识
		if ($title) {
			$i = 0;
			foreach ($title as $v) {
				//设置列标题
				$activeSheet->setCellValue($cellName[$i] . $_row, $v);
				$i++;
			}
			$_row++;
		}

		foreach ($applylst as $key => $val) {

			//填写数据
			if ($val) {
				unset($val['personalpic']);
				unset($val['doctorpic']);
				unset($val['license']);
				$i = 0;
				foreach ($val as $_v) {
					$activeSheet->setCellValueExplicit($cellName[$i] . ($_row), $_v, \PHPExcel_Cell_DataType::TYPE_STRING);
					$i++;
				}
			}
			$_row++;
		}

		//文件名处理
		if (!$fileName) {
			$fileName = uniqid(time(), true);
		}

		$objWrite = \PHPExcel_IOFactory::createWriter($obj, 'Excel5');

		//网页下载
		header('pragma:public');
		header("Content-Disposition:attachment;filename=$fileName.xls");
		$objWrite->save('php://output');exit;
	}

	public function export_personpic() {
		$data = model("common/Apply")->where('status', 2)->column("CONCAT('../public/uploads/',personalpic)");
		if ($data) {
			$re = zip($data);
		} else {
			$this->error("暂无数据");
		}

	}

}
